﻿Imports DevExpress.XtraEditors
Imports DevExpress.XtraGrid
Imports DevExpress.XtraTreeList
Imports DevExpress.XtraTreeList.Nodes

Public Class frmDuToan

    Private Sub LoadTree()
        With Me.TreeList1
            .OptionsBehavior.Editable = False
            .OptionsView.ShowColumns = False
            .OptionsView.ShowHorzLines = False
            .OptionsView.ShowVertLines = False
            .OptionsView.ShowIndicator = False

            Me.TreeListColumn1.Visible = False
            Me.TreeListColumn2.Width = 250
            Me.TreeListColumn3.Visible = False

            Dim Node As TreeListNode = Nothing
            Dim Node0 As TreeListNode = Nothing
            Dim Node1 As TreeListNode = Nothing

            .Nodes.Clear()

            Dim myDtF As New DuToanF
            Dim myDtb As New DataTable
            Dim i As Integer

            'all
            Node0 = Me.TreeList1.AppendNode(New Object() {0, "Dự toán năm " + Project_Nam.ToString, ""}, Node)

            'don vi
            myDtb = myDtF.Get_List_DonVi
            For i = 0 To myDtb.Rows.Count - 1
                Node1 = .AppendNode(New Object() {1, myDtb.Rows(i).Item("madv") + "   " + myDtb.Rows(i).Item("tendv"), myDtb.Rows(i).Item("madv")}, Node0)
            Next

            .ExpandAll()
        End With


    End Sub

    Private Sub InitGrid()
        Dim i As Integer
        With Me.GridView1
            i = 0
            .Columns(i).FieldName = "sodt"
            .Columns(i).Caption = "Số dự toán"
            .Columns(i).Width = 40

            i = 1
            .Columns(i).FieldName = "ngaylap"
            .Columns(i).Caption = "Ngày lập"
            .Columns(i).Width = 40

            i = 2
            .Columns(i).FieldName = "tenloaichiettinh"
            .Columns(i).Caption = "Loại chiết tính"
            .Columns(i).Width = 60

            i = 3
            .Columns(i).FieldName = "tenloaidt"
            .Columns(i).Caption = "Loại dự toán"
            .Columns(i).Width = 60

            i = 4
            .Columns(i).FieldName = "maloaichiettinh"
            .Columns(i).Visible = False

            i = 5
            .Columns(i).FieldName = "maloaidt"
            .Columns(i).Visible = False

        End With


    End Sub

    Private Sub LoadGrid()
        If Me.TreeList1.FocusedNode Is Nothing Then Exit Sub

        Dim myDtF As New DuToanF
        Dim myDtb As New DataTable
        myDtb = myDtF.Get_Filter(Me.TreeList1.FocusedNode.Item(2))
        Me.GridControl1.DataSource = myDtb

        Me.GridView1.ExpandAllGroups()
    End Sub

    Private Sub frmDuToan_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
        frmDuToan_Exists = False
    End Sub

    Private Sub frmDuToan_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        InitGrid()
        LoadTree()
    End Sub

    Private Sub TreeList1_FocusedNodeChanged(ByVal sender As Object, ByVal e As DevExpress.XtraTreeList.FocusedNodeChangedEventArgs) Handles TreeList1.FocusedNodeChanged
        LoadGrid()
    End Sub

    Private Sub btnThemDuToan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThemDuToan.Click
        If frmDuToan_CapNhat_Exists Then Exit Sub
        frmDuToan_CapNhat_Exists = True

        Dim f As New frmDuToan_CapNhat
        f.txtFormParent.EditValue = Me.Name
        f.txtAddEdit.EditValue = "add"
        f.Text = "Lập dự toán vật tư"

        f.MdiParent = _frmMenu
        f.Show()
    End Sub

    Private Sub btnSuaDuToan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSuaDuToan.Click
        If frmDuToan_CapNhat_Exists Then Exit Sub
        frmDuToan_CapNhat_Exists = True

        Dim f As New frmDuToan_CapNhat
        f.txtFormParent.EditValue = Me.Name
        f.txtAddEdit.EditValue = "edit"
        f.txtSoDt.EditValue = Me.GridView1.GetFocusedRowCellValue("sodt")
        f.Text = "Sửa dự toán vật tư"

        f.MdiParent = _frmMenu
        f.Show()
    End Sub

    Private Sub GridView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.DoubleClick
        If frmDuToan_CapNhat_Exists Then Exit Sub
        frmDuToan_CapNhat_Exists = True

        Dim f As New frmDuToan_CapNhat
        f.txtFormParent.EditValue = Me.Name
        f.txtAddEdit.EditValue = "edit"
        f.txtSoDt.EditValue = Me.GridView1.GetFocusedRowCellValue("sodt")
        f.Text = "Sửa dự toán vật tư"

        f.MdiParent = _frmMenu
        f.Show()
    End Sub

End Class